package com.xmtlxx.system.service.impl;

import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.entity.SysUser;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.helper.LoginHelper;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.common.utils.push.domain.PushContent;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmtlxx.system.domain.NoticeUser;
import com.xmtlxx.system.domain.SysNotice;
import com.xmtlxx.system.domain.vo.NoticeUserVo;
import com.xmtlxx.system.mapper.NoticeUserMapper;
import com.xmtlxx.system.mapper.SysNoticeMapper;
import com.xmtlxx.system.mapper.SysUserMapper;
import com.xmtlxx.system.service.ISysNoticeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 公告 服务层实现
 *
 * @author xmtlxx
 */
@RequiredArgsConstructor
@Service
public class SysNoticeServiceImpl implements ISysNoticeService {

    private final SysNoticeMapper baseMapper;

    private final NoticeUserMapper noticeUserMapper;

    private final SysUserMapper userMapper;

    @Override
    public TableDataInfo<SysNotice> selectPageNoticeList(SysNotice notice, PageQuery pageQuery) {
        LambdaQueryWrapper<SysNotice> lqw = new LambdaQueryWrapper<SysNotice>()
            .like(StringUtils.isNotBlank(notice.getNoticeTitle()), SysNotice::getNoticeTitle, notice.getNoticeTitle())
            .eq(StringUtils.isNotBlank(notice.getNoticeType()), SysNotice::getNoticeType, notice.getNoticeType())
            .like(StringUtils.isNotBlank(notice.getCreateBy()), SysNotice::getCreateBy, notice.getCreateBy());
        if(LoginHelper.isTenantAdmin()){
            lqw.eq(SysNotice::getTenantId,LoginHelper.getTenantId());
        }
        lqw.orderByDesc(SysNotice::getCreateTime);
        Page<SysNotice> page = baseMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId) {
        return baseMapper.selectById(noticeId);
    }

    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice) {
        return baseMapper.selectList(new LambdaQueryWrapper<SysNotice>()
            .like(StringUtils.isNotBlank(notice.getNoticeTitle()), SysNotice::getNoticeTitle, notice.getNoticeTitle())
            .eq(StringUtils.isNotBlank(notice.getNoticeType()), SysNotice::getNoticeType, notice.getNoticeType())
            .like(StringUtils.isNotBlank(notice.getCreateBy()), SysNotice::getCreateBy, notice.getCreateBy()));
    }

    /**
     * 新增公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int insertNotice(SysNotice notice,Long[] posts,Long[] users) {
        int row = baseMapper.insert(notice);
        insertNoticeUser(notice,posts,users);
        return row;
    }

    /**
     * 修改公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNotice notice,Long[] posts,Long[] users) {
        int row = baseMapper.updateById(notice);
        insertNoticeUser(notice,posts,users);
        return row;
    }

    public void insertNoticeUser(SysNotice notice,Long[] posts,Long[] users){
        noticeUserMapper.delete(new LambdaQueryWrapper<NoticeUser>().eq(NoticeUser::getNoticeId,notice.getNoticeId()));
        List<Long> userIds = noticeUserMapper.selectUserIds(notice.getDeptId(),posts,users);
        if(userIds != null && userIds.size() > 0){
            List<NoticeUser> items = new ArrayList<>();
            for(Long userId : userIds){
                NoticeUser noticeUser = new NoticeUser();
                noticeUser.setNoticeId(notice.getNoticeId());
                noticeUser.setUserId(userId);
                noticeUser.setStatus("0");
                items.add(noticeUser);
            }
            noticeUserMapper.insertBatch(items);
        }
    }

    /**
     * 删除公告对象
     *
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Long noticeId) {
        return baseMapper.deleteById(noticeId);
    }

    /**
     * 批量删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(Long[] noticeIds) {
        return baseMapper.deleteBatchIds(Arrays.asList(noticeIds));
    }

    /**
     * 查询公告用户列表
     *
     * @param noticeUser 公告用户
     * @param pageQuery
     * @return 公告用户集合
     */
    @Override
    public TableDataInfo<NoticeUserVo> selectPageNoticeUserList(NoticeUserVo noticeUser, PageQuery pageQuery) {
        Page<NoticeUserVo> page = noticeUserMapper.selectPageNoticeUserList(pageQuery.build(),noticeUser);
        return TableDataInfo.build(page);
    }

    @Override
    public void sendNotice(String type,List<String> toUsers, Long deptId, PushContent content,String contentInfo) {
        if(toUsers != null && toUsers.size() > 0){
            SysNotice notice = new SysNotice();
            notice.setNoticeType(type);
            notice.setNoticeTitle(content.getTitle());
            notice.setDeptId(deptId);
            notice.setNoticeContent(contentInfo);
            notice.setStatus("0");
            notice.setUserIds(String.join(",",toUsers));
            baseMapper.insert(notice);

            toUsers.forEach(item->{
                NoticeUser user = new NoticeUser();
                user.setUserId(Long.valueOf(item));
                user.setNoticeId(notice.getNoticeId());
                user.setStatus("0");
                noticeUserMapper.insert(user);
                content.setRequestId(user.getNoticeUserId().toString());
                SysUser sysUser = userMapper.selectUserById(user.getUserId());
                if(StringUtils.isNotBlank(sysUser.getDeviceId())){
                    try{
//                        PushUtils.pushMsg(sysUser.getDeviceId(), content);
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                }
            });
        }
    }
}
